﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;

namespace MvcApplication4.Controllers
{
    public class DependenteController : Controller
    {
        private BD_Vacinas2Context db = new BD_Vacinas2Context();

        //
        // GET: /Dependente/

        public ActionResult Index()
        {
            var tb_dependente = db.tb_Dependente.Include(t => t.tb_Paciente);
            return View(tb_dependente.ToList());
        }

        //
        // GET: /Dependente/Details/5

        public ActionResult Details(int id = 0)
        {
            tb_Dependente tb_dependente = db.tb_Dependente.Find(id);
            if (tb_dependente == null)
            {
                return HttpNotFound();
            }
            return View(tb_dependente);
        }

        //
        // GET: /Dependente/Create

        public ActionResult Create()
        {
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente");
            return View();
        }

        public ActionResult CreateAposCadastro()
        {

            var pac = from s in db.tb_Paciente
                      select s;

            var model = TempData["tempnome"] as string;

            pac = pac.Where(s => s.Nome_Paciente.Contains(model));

            ViewBag.ID_Paciente = new SelectList(pac, "ID_Paciente", "Nome_Paciente");
            return View();
        }

        //
        // POST: /Dependente/Create

        [HttpPost]
        public ActionResult Create(tb_Dependente tb_dependente)
        {
            if (ModelState.IsValid)
            {
                db.tb_Dependente.Add(tb_dependente);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente", tb_dependente.ID_Paciente);
            return View(tb_dependente);
        }

        //
        // GET: /Dependente/Edit/5

        public ActionResult Edit(int id = 0)
        {
            tb_Dependente tb_dependente = db.tb_Dependente.Find(id);
            if (tb_dependente == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente", tb_dependente.ID_Paciente);
            return View(tb_dependente);
        }

        //
        // POST: /Dependente/Edit/5

        [HttpPost]
        public ActionResult Edit(tb_Dependente tb_dependente)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_dependente).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente", tb_dependente.ID_Paciente);
            return View(tb_dependente);
        }

        //
        // GET: /Dependente/Delete/5

        public ActionResult Delete(int id = 0)
        {
            tb_Dependente tb_dependente = db.tb_Dependente.Find(id);
            if (tb_dependente == null)
            {
                return HttpNotFound();
            }
            return View(tb_dependente);
        }

        //
        // POST: /Dependente/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tb_Dependente tb_dependente = db.tb_Dependente.Find(id);
            db.tb_Dependente.Remove(tb_dependente);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}